package com.zyy.smartcity1129.me;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.Http;

import okhttp3.RequestBody;

/**
 * @ClassName: ChangePwd$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 1/12/2021$
 * @Version: 1.0
 */
public class ChangePwdFragment extends BaseFragment {
    private LinearLayout lineMyData1;
    private ImageView imgMyData;
    private LinearLayout lineMyData2;
    private TextView tvMyData1;
    private EditText tvMyDataNick;
    private LinearLayout lineMyData3;
    private Spinner spMySex;
    private LinearLayout lineMyData4;
    private TextView tvMyData;
    private EditText tvMyPhone;
    private Button btnMyReplace;

    @Override
    protected int layId() {
        return R.layout.f_mydata;
    }

    @Override
    protected String title() {
        return "修改密码";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        btnMyReplace.setOnClickListener(view -> {
            String old = tvMyData1.getText().toString().trim();
            String mNew = tvMyData.getText().toString().trim();
            if (!old.equals("") && !mNew.equals("")) {
                if (!old.equals(mNew)) {
                    th(() ->{
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("newPassword", mNew);
                        jsonObject.addProperty("oldPassword", old);
                        String result = Http.getInstance().get("/prod-api/api/common/user/resetPwd", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 3).getMsg();
                        ui(() ->{
                            Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show();
                            remove();
                        });
                    });
                } else {
                    Toast.makeText(getContext(), "两次输入密码一样", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getContext(), "密码为空", Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initView(View iv) {
        lineMyData1 = (LinearLayout) iv.findViewById(R.id.lineMyData1);
        imgMyData = (ImageView) iv.findViewById(R.id.imgMyData);
        lineMyData2 = (LinearLayout) iv.findViewById(R.id.lineMyData2);
        tvMyData1 = (TextView) iv.findViewById(R.id.tvMyData1);
        tvMyDataNick = (EditText) iv.findViewById(R.id.tvMyDataNick);
        lineMyData3 = (LinearLayout) iv.findViewById(R.id.lineMyData3);
        spMySex = (Spinner) iv.findViewById(R.id.spMySex);
        lineMyData4 = (LinearLayout) iv.findViewById(R.id.lineMyData4);
        tvMyData = (TextView) iv.findViewById(R.id.tvMyData);
        tvMyPhone = (EditText) iv.findViewById(R.id.tvMyPhone);
        btnMyReplace = (Button) iv.findViewById(R.id.btnMyReplace);
        lineMyData2.setVisibility(View.GONE);
        lineMyData3.setVisibility(View.GONE);
        tvMyData1.setText("原密码：");
        tvMyData.setText("新密码：");
    }
}
